/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server.dao;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import org.hibernate.Session;
import server.domain.Client;
import server.domain.Order;
import server.domain.OrderItem;
import server.domain.Product;
import server.util.DBUtil;

/**
 *
 * @author fillipe
 */
public class OrderDAO extends GenericDAO<Order> {

    public OrderDAO() {
        super(Order.class);
    }

    public void save(Order order, List<OrderItem> orderItems) throws Exception {
        Session session = DBUtil.getCurrentSession();
        try {
            session.beginTransaction();
            order = (Order) session.merge(order);
            BigDecimal total = new BigDecimal(0);
            for (OrderItem oi : orderItems) {
                total = total.add(oi.getTotalItem().subtract(oi.getDiscount()));
                oi.getId().setOrder(order);
                session.save(oi);
            }
            order.setTotalOrder(total);
            session.merge(order);
            session.getTransaction().commit();
        } catch (Exception ex) {
            session.getTransaction().rollback();
            throw ex;
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }
    }

    public void update(Order order, List<OrderItem> orderItems) throws Exception {
        Session session = DBUtil.getCurrentSession();
        try {
            session.beginTransaction();
            BigDecimal total = new BigDecimal(0);
            for (OrderItem oi : orderItems) {
                if (oi.getQuantity() > 0) {
                    total = total.add(oi.getTotalItem().subtract(oi.getDiscount()));
                    session.merge(oi);
                } else {
                    session.delete(oi);
                }
            }
            order.setTotalOrder(total);
            session.merge(order);
            session.getTransaction().commit();
        } catch (Exception ex) {
            session.getTransaction().rollback();
            throw ex;
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }
    }

    public void deleteOrder(Order order) throws Exception {
        List<OrderItem> items = list("from OrderItem oi where oi.id.order = ?", order);
        Session session = DBUtil.getCurrentSession();
        try {
            session.beginTransaction();
            for (OrderItem item : items) {
                session.delete(item);
            }
            session.delete(order);
            session.getTransaction().commit();
        } catch (Exception ex) {
            session.getTransaction().rollback();
            throw ex;
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }
    }

    public OrderItem getItem(Order order, Product product) {
        return (OrderItem) getOne("from OrderItem oi where oi.id.order = ? and oi.id.product = ?", order, product);
    }

    public List<OrderItem> getItems(Order order) {
        return list("from OrderItem oi where oi.id.order = ?", order);
    }

    public List<Order> getOrdersByClient(Client client) {
        return list("from Order o where o.client = ?", client);
    }

    public void updateOrderItem(OrderItem oi) throws Exception {
        Session session = DBUtil.getCurrentSession();
        try {
            session.beginTransaction();
            session.update(oi);
            session.getTransaction().commit();
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }
    }
}
